package com.lx.screenshot.ui.setting;

import android.os.Bundle;

import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.lx.screenshot.R;
import com.lx.screenshot.util.DrawableUtil;
import com.lx.screenshot.util.SettingUtil;

public class SettingsFragment extends PreferenceFragmentCompat {

    private PreferenceManager manager;
    private Preference floatWinIcon;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);

        init();

        findPre();

        initView();

        addListener();
    }

    private void findPre() {
        floatWinIcon = manager.findPreference("floatWinIcon");
    }

    private void initView() {
        if (floatWinIcon != null)
            floatWinIcon.setIcon(
                    DrawableUtil.parseResPathToInt(getContext(), SettingUtil.getSetting(getContext(), "floatWinIcon", "res/drawable-v24/da_sha_gou_gou_icon.png"))
            );
    }

    private void addListener() {
        if (floatWinIcon != null) {
            floatWinIcon.setOnPreferenceChangeListener((preference, newValue) -> {
                floatWinIcon.setIcon(DrawableUtil.parseResPathToInt(getContext(), String.valueOf(newValue)));

                return true;
            });
        }
    }

    private void init() {
        manager = this.getPreferenceManager();
    }
}